<?php

namespace app\controller\notify;

use app\BaseController;
use think\facade\Cache;
use app\lib\Key;

/**
 * 微信支付回调
 */
class Weixin extends BaseController {

    public $payType = 'weixin';

    /**
     * 微信回调处理
     */
    public function index() {
        // 获取流式数据
        $data = $this->request->getInput();
        debugLog($data);
        // 1：解析数据，校验数据是否 订单 => redis
        $jsonXml = json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA));
        // 转成数组
        $result = json_decode($jsonXml, true);
        if ($result && isset($result['result_code'])
            && isset($result['return_code'])
            && $result['result_code'] == 'SUCCESS'
            && $result['return_code'] == 'SUCCESS'
        ) {
            //$result = Cache::store('redis')->hGet(Key::Order($appId), $orderId);
            $redisResult = Cache::store('redis')->hGet(Key::Order($result['attach']), $result['out_trade_no']);
            if ($redisResult) {
                $redisArr = json_decode($redisResult, true);
                if ($redisArr['pay_status'] == 0) {
                    $redisArr['pay_status'] = 1;
                    $redisArr['pay_time'] = time();
                    $redisArr['transaction_id'] = $result['transaction_id'];
                    Cache::store('redis')->hSet(Key::Order($result['attach']), $result['out_trade_no'], json_encode($redisArr));
                }
            } else {
                $redisArr = [
                    'pay_status'     => 1,
                    'pay_time'       => time(),
                    'transaction_id' => $result['transaction_id'],
                ];
                Cache::store('redis')->hSet(Key::Order($result['attach']), $result['out_trade_no'], json_encode($redisArr));
                debugLog('notify-redis-notfound: ' . $result['out_trade_no'], 'wx.pay.error.log');
            }

            // 2: 告诉微信，success ok,  更新redis hash 支付状态，添加支付时间、微信订单号
            echo '<xml>
              <return_code><![CDATA[SUCCESS]]></return_code>
              <return_msg><![CDATA[OK]]></return_msg>
                  </xml>';
            exit;
        }
        echo "fail";
        exit;
    }

//     <xml><appid><![CDATA[wx60f11480dce266ab]]></appid>
//    <bank_type><![CDATA[OTHERS]]></bank_type>
//    <cash_fee><![CDATA[12]]></cash_fee>
//    <fee_type><![CDATA[CNY]]></fee_type>
//    <is_subscribe><![CDATA[Y]]></is_subscribe>
//    <mch_id><![CDATA[1571949741]]></mch_id>
//    <nonce_str><![CDATA[3zzmo1075uj2shgt76pdrxh7ntdove2w]]></nonce_str>
//    <openid><![CDATA[oJmWawUNTJpPXqdvPt5PzQ9uVxxc]]></openid>
//    <out_trade_no><![CDATA[1111m1gd122sgsg253463]]></out_trade_no>
//    <result_code><![CDATA[SUCCESS]]></result_code>
//    <return_code><![CDATA[SUCCESS]]></return_code>
//    <sign><![CDATA[4AC3A4046BC1C61A1D518D6B12F7CC9CA0D48D426710BA0C0E28F9FE4635FC1E]]></sign>
//    <time_end><![CDATA[20200223124139]]></time_end>
//    <total_fee>12</total_fee>
//    <trade_type><![CDATA[NATIVE]]></trade_type>
//    <transaction_id><![CDATA[4200000508202002239368222928]]></transaction_id>
//    </xml>
}
